﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace BuildRunner
{
	public class VS2010Sln
	{
		public CompilerType Type { get; set; }
		public List<CProject> Project { get; set; }
		public List<CPattern> Pattern { get; set; }
		public List<string> Setting { get; set; }
		public class CProject
		{
			public string ProjectID { get; set; }
			public string Name { get; set; }
			public string FileName { get; set; }
			public string ID { get; set; }
		}
		public class CPattern
		{
			public string Name1 { get; set; }
			public string Name2 { get; set; }
		}
		[System.Xml.Serialization.XmlIgnore]
		static Dictionary<string,CompilerType> m_dicCompiler = new Dictionary<string, CompilerType>() { 
		{ "11.00", CompilerType.VS2010 }, { "10.00", CompilerType.VS2008 }, };

		public static VS2010Sln Analyse( string strFilePath )
		{
			VS2010Sln retData = new VS2010Sln();
			string strText = File.ReadAllText( strFilePath );
			Regex reg = new Regex( "Microsoft Visual Studio Solution File, Format Version (?<version>[0-9.]*)" );
			var match = reg.Match( strText );
			if( !match.Success ) 
				return null;
			string strVersion = match.Groups["version"].Value;
			if( !m_dicCompiler.ContainsKey( strVersion ) )
				return null;
			retData.Type = m_dicCompiler[strVersion];
			strText = strText.Remove( match.Index, match.Length );

			int iRemoveLength = 0;
			Regex regProject = new Regex( "Project\\(\"(?<project>\\{[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}\\})\"\\) = \"(?<name>.*?)\", \"(?<filename>.*?)\", \"(?<id>\\{[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}\\})\"\\r\\nEndProject"
				, RegexOptions.Multiline );
			var matchProject = regProject.Matches( strText );
			retData.Project = new List<CProject>();
			foreach( Match mc in matchProject ) {
				string strProject = mc.Groups["project"].Value;
				string strName = mc.Groups["name"].Value;
				string strFileName = mc.Groups["filename"].Value;
				string strId = mc.Groups["id"].Value;
				strText = strText.Remove( mc.Index - iRemoveLength, mc.Length );
				iRemoveLength += mc.Length;
				retData.Project.Add( new CProject() {
					ProjectID = strProject, Name = strName, FileName = strFileName, ID = strId } );
			}

			Regex regSection = new Regex( "GlobalSection\\(SolutionConfigurationPlatforms\\) = preSolution(?<params>(.|\\r\\n)*?)EndGlobalSection"
				, RegexOptions.Singleline );
			var matchSection = regSection.Match( strText );
			Regex regSelectionPattern = new Regex( "^\\t*(?<name1>.*?)=(?<name2>.*?)$"
				, RegexOptions.Multiline );
			var matchSelectionPattern = regSelectionPattern.Matches( matchSection.Groups["params"].Value );
			retData.Pattern = new List<CPattern>();
			retData.Setting = new List<string>();
			foreach( Match mc in matchSelectionPattern ) {
				string strName1 = mc.Groups["name1"].Value.Trim().Replace( "\r", "" );
				string strName2 = mc.Groups["name2"].Value.Trim().Replace( "\r", "" );
				retData.Pattern.Add( new CPattern() { Name1 = strName1, Name2 = strName2 } );
				retData.Setting.Add( strName1 );
			}

			return retData;
		}
	}
}
